﻿using Newtonsoft.Json.Linq;
using PDFDesign.Model;
using PDFDesign.Properties;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace PDFDesign.ViewModel
{
    public class PageSettingViewModel : BaseViewModel
    {
        private readonly Dictionary<string, List<double>> PDFSizeDic = new Dictionary<string, List<double>>();

        public PageSettingViewModel(PageSetting pageSetting)
        {
            PageSetting = pageSetting;
            width = pageSetting.Size[0];
            height = pageSetting.Size[1];
            isPortrait = pageSetting.Layout == Layout.portrait;
            reportCount = pageSetting.ReportCount;
            JToken.Parse(Resources.PDFSizes).Children().ToList().ForEach(o =>
            {
                var prop = o as JProperty;
                pdfSizes.Add(prop.Name);
                PDFSizeDic.Add(prop.Name, prop.Value.ToObject<List<double>>());
            });
        }

        public PageSetting PageSetting { get; set; }

        private double width = 595.28;
        /// <summary>
        /// 宽
        /// </summary>
        public double Width
        {
            get => width;
            set
            {
                width = value;
                OnPropertyChanged("Width");
            }
        }

        private double height = 841.89;
        /// <summary>
        /// 高
        /// </summary>
        public double Height
        {
            get => height;
            set
            {
                height = value;
                OnPropertyChanged("Height");
            }
        }

        private List<string> pdfSizes = new List<string>();
        /// <summary>
        /// 页面大小列表
        /// </summary>
        public List<string> PDFSizes
        {
            get => pdfSizes;
            set
            {
                pdfSizes = value;
                OnPropertyChanged("PDFSizes");
            }
        }

        private string selectedSize = "A4";
        /// <summary>
        /// 选择的页面大小
        /// </summary>
        public string SelectedSize
        {
            get => selectedSize;
            set
            {
                selectedSize = value;
                if (!string.IsNullOrWhiteSpace(selectedSize))
                {
                    var list = PDFSizeDic[value];
                    Width = list[0];
                    Height = list[1];
                }
                OnPropertyChanged("SelectedSize");
            }
        }

        private bool enabledMode = false;
        /// <summary>
        /// 启用大小模式
        /// </summary>
        public bool EnabledMode
        {
            get => enabledMode;
            set
            {
                enabledMode = value;
                if (value)
                {
                    ModeVisibility = Visibility.Visible;
                    SizeVisibility = Visibility.Collapsed;
                }
                else
                {
                    ModeVisibility = Visibility.Collapsed;
                    SizeVisibility = Visibility.Visible;
                }
                OnPropertyChanged("EnabledMode");
            }
        }

        private Visibility modeVisibility = Visibility.Collapsed;
        public Visibility ModeVisibility
        {
            get => modeVisibility;
            set
            {
                modeVisibility = value;
                OnPropertyChanged("ModeVisibility");
            }
        }

        private Visibility sizeVisibility;
        public Visibility SizeVisibility
        {
            get => sizeVisibility;
            set
            {
                sizeVisibility = value;
                OnPropertyChanged("SizeVisibility");
            }
        }

        private bool isPortrait = true;
        /// <summary>
        /// 是否为纵向
        /// </summary>
        public bool IsPortrait
        {
            get => isPortrait;
            set
            {
                isPortrait = value;
                OnPropertyChanged("IsPortrait");
            }
        }

        private int reportCount = 1;
        /// <summary>
        /// 每页报表数量
        /// </summary>
        public int ReportCount
        {
            get => reportCount;
            set
            {
                reportCount = value;
                OnPropertyChanged("ReportCount");
            }
        }

        public void Save()
        {
            PageSetting.Size = new double[] { Width, Height };
            PageSetting.Layout = IsPortrait ? Layout.portrait : Layout.landscape;
            PageSetting.ReportCount = ReportCount;
        }
    }
}
